package java111;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Day14 {
    // https://www.nowcoder.com/questionTerminal/bb4f1a23dbb84fd7b77be1fbe9eaaf32
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s1 = in.next();
            String s2 = in.next();
            HashMap<Character, Integer> hashMap = new HashMap<>();
            for (int i = 0; i < s1.length(); i++) {
                hashMap.put(s1.charAt(i), hashMap.getOrDefault(s1.charAt(i), 0) + 1);
            }
            int i = 0;
            for (; i < s2.length(); i++) {
                char ch = s2.charAt(i);
                if (!hashMap.containsKey(ch)) {
                    System.out.println("No");
                    break;
                } else {
                    hashMap.put(ch, hashMap.get(ch) - 1);
                    if (hashMap.get(ch) == 0) {
                        hashMap.remove(ch);
                    }
                }
            }
            if (i == s2.length()) {
                System.out.println("Yes");
            }
        }
    }


    //https://www.nowcoder.com/questionTerminal/6736cc3ffd1444a4a0057dee89be789b?orderByHotValue=1&page=1&onlyReference=false
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int[] arr = new int[3 * n];
        for (int i = 0; i < 3 * n; i++) {
            arr[i] = in.nextInt();
        }
        Arrays.sort(arr);
        long res = 0;
        for (int i = arr.length - 2; i >= 0; i -= 2) {
            res += arr[i];
            if (--n <= 0) {
                break;
            }
        }
        System.out.println(res);
    }


    //https://www.nowcoder.com/practice/3bcf72c738b6494bbe1ebe0ffde56152?tpId=230&tqId=40419&ru=/exam/oj
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int[] arr = new int[n];
            for(int i = 0; i < arr.length; i++){
                arr[i] = in.nextInt();
            }
            int[] count = new int[10001];
            for(int i = 0; i < arr.length; i++){
                count[arr[i]] += arr[i];
            }
            int[] f = new int[10001]; // f[i]表示，选到i位置时，选中第i个元素时的最大分数
            int[] g = new int[10001]; // g[i]表示，选到i位置时，不选第i个元素时的最大分数
            f[0] = g[0] = 0;
            for(int i = 1; i < f.length; i++){
                f[i] = g[i-1] + count[i];
                g[i] = Math.max(g[i-1], f[i-1]);
            }
            System.out.println(Math.max(f[10000], g[10000]));
        }
    }
}
